<template>
    <div class="box">
        <div class="title"><span>答题人数</span><span>{{peopleN}}人</span></div>
        <div class="aa">
            <div class="content">
                <div class="content_item" :style="{'width':item.per+'%'}" v-for="(item,index) in data" :key="index">
                </div>
            </div>
            <div class="text_box">
                <div class="text" v-for="(item,index) in data" :key="index">
                    <div class="block" :style="{background:comStyle(item.index)}"></div>
                    <span>{{ item.name }}:{{item.per}}%</span><span></span>(参与{{item.value}} 人)</span>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    props: ["boxWidth", "data", "peopleN"],
    watch: {
        peopleN: {
            deep: true,
            handler() {
                this.peopleNum = this.peopleN
            }
        },
        data: {
            deep: true,
            immediate: true,
            handler() {
                this.data.forEach((item, index) => {
                    item.index = index
                })
            }
        }
    },
    data() {
        return {
            peopleNum: null,
        }
    },
    mounted() {
        this.data.forEach((item, index) => {
            item.index = index
        })
    },
    computed: {
        comStyle() {
            return (index) => {
                switch (index) {
                    case 0:
                        return "linear-gradient(90deg, #006aff, #00bcf6)"
                    case 1:
                        return "linear-gradient(90deg, #e2ce73, #e2a016)"
                    case 2:
                        return "linear-gradient(90deg, #d35bf8, #9417e5)"
                    case 3:
                        return "linear-gradient(90deg, #7bf2b7, #38b174)"
                }
            }
        }
    }
};
</script>

<style scoped>
.box {
    width: 100%;
    display: flex;
    align-items: self-start;
}
.title {
    font-size: 1.6rem;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #222222;
    display: flex;
    flex-direction: column;
    align-items: center;
    flex-shrink: 0;
    margin-right: 35px;
}
.title span:last-child {
    color: #006fed;
}
.aa {
    width: 100%;
    position: relative;
}
.content {
    display: flex;
    align-items: center;
    width: 100%;
    height: 2rem;
    box-sizing: border-box;

    position: relative;
}
.content_item {
    height: 100%;
}
.content .content_item:first-child {
    background: linear-gradient(90deg, #006aff, #00bcf6);
}
.content .content_item:nth-child(2) {
    background: linear-gradient(90deg, #e2ce73, #e2a016);
}
.content .content_item:nth-child(3) {
    background: linear-gradient(90deg, #d35bf8, #9417e5);
}
.content .content_item:nth-child(4) {
    background: linear-gradient(90deg, #7bf2b7, #38b174);
}
.text_box {
    width: 100%;
    font-size: 1.6rem;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #222222;
    display: flex;
    position: absolute;
}
.text {
    /* transform: translateY(50%); */
    width: calc(100% / 3);
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
}
.block {
    width: 2.5rem;
    height: 0.5rem;
    margin-top: 1rem;
}
</style>
